﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
namespace Sbook.Data
{
    public class UserController:SqlDataProvider
    {
        #region[User_GetById]
        public List<UserInfo> User_GetById(string Id)
        {
            List<Data.UserInfo> list = new List<Data.UserInfo>();
            using (SqlCommand dbCmd = new SqlCommand("sp_User_GetById", GetConnection()))
            {
                Data.UserInfo obj = new Data.UserInfo();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@UId", Id));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.UserIDataReader(dr));
                    }
                    dr.Close();
                    //conn.Close();
                }
            }
            return list;
        }
        #endregion
        #region[User_GetByTop]
        public List<UserInfo> User_GetByTop(string Top, string Where, string Order)
        {
            List<Data.UserInfo> list = new List<Data.UserInfo>();
            using (SqlCommand dbCmd = new SqlCommand("sp_User_GetByTop", GetConnection()))
            {
                Data.UserInfo obj = new Data.UserInfo();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Top", Top));
                dbCmd.Parameters.Add(new SqlParameter("@Where", Where));
                dbCmd.Parameters.Add(new SqlParameter("@Order", Order));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.UserIDataReader(dr));
                    }
                    dr.Close();
                    //conn.Close();
                }
            }
            return list;
        }
        #endregion
        #region[User_GetByAll]
        public List<UserInfo> User_GetByAll()
        {
            List<Data.UserInfo> list = new List<Data.UserInfo>();
            using (SqlCommand dbCmd = new SqlCommand("sp_User_GetByAll", GetConnection()))
            {
                Data.UserInfo obj = new Data.UserInfo();
                dbCmd.CommandType = CommandType.StoredProcedure;
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.UserIDataReader(dr));
                    }
                    dr.Close();
                    //conn.Close();
                }
            }
            return list;
        }
        #endregion
        #region[User_Paging]
        public List<UserInfo> User_Paging(string CurentPage, string PageSize)
        {
            List<Data.UserInfo> list = new List<Data.UserInfo>();
            using (SqlCommand dbCmd = new SqlCommand("sp_User_Paging", GetConnection()))
            {
                Data.UserInfo obj = new Data.UserInfo();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@CurentPage", CurentPage));
                dbCmd.Parameters.Add(new SqlParameter("@PageSize", PageSize));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.UserIDataReader(dr));
                    }
                    dr.Close();
                    //conn.Close();
                }
            }
            return list;
        }
        #endregion
        #region[User_Insert]
        public bool User_Insert(UserInfo data)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_User_Insert", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Username", data.Username));
                dbCmd.Parameters.Add(new SqlParameter("@Pass", data.Pass));
                dbCmd.Parameters.Add(new SqlParameter("@isAdmin", data.isAdmin));
                dbCmd.Parameters.Add(new SqlParameter("@Fullname", data.Fullname));
                dbCmd.Parameters.Add(new SqlParameter("@Provenumber", data.Provenumber));
                dbCmd.Parameters.Add(new SqlParameter("@Address", data.Address));
                dbCmd.Parameters.Add(new SqlParameter("@Email", data.Email));
                dbCmd.Parameters.Add(new SqlParameter("@Phone", data.Phone));
                dbCmd.Parameters.Add(new SqlParameter("@Class", data.Class));
                dbCmd.Parameters.Add(new SqlParameter("@University", data.University));
                dbCmd.Parameters.Add(new SqlParameter("@Status", data.Status));
                dbCmd.Parameters.Add(new SqlParameter("@MailTo", data.MailTo));
                dbCmd.Parameters.Add(new SqlParameter("@FullNameTo", data.FullNameTo));
                dbCmd.Parameters.Add(new SqlParameter("@AddressTo", data.AddressTo));
                dbCmd.Parameters.Add(new SqlParameter("@PhoneTo", data.PhoneTo));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("User");
            return true;
        }
        #endregion
        #region[User_Update]
        public bool User_Update(UserInfo data)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_User_Update", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@UId", data.UId));
                dbCmd.Parameters.Add(new SqlParameter("@Username", data.Username));
                dbCmd.Parameters.Add(new SqlParameter("@Pass", data.Pass));
                dbCmd.Parameters.Add(new SqlParameter("@isAdmin", data.isAdmin));
                dbCmd.Parameters.Add(new SqlParameter("@Fullname", data.Fullname));
                dbCmd.Parameters.Add(new SqlParameter("@Provenumber", data.Provenumber));
                dbCmd.Parameters.Add(new SqlParameter("@Address", data.Address));
                dbCmd.Parameters.Add(new SqlParameter("@Email", data.Email));
                dbCmd.Parameters.Add(new SqlParameter("@Phone", data.Phone));
                dbCmd.Parameters.Add(new SqlParameter("@Class", data.Class));
                dbCmd.Parameters.Add(new SqlParameter("@University", data.University));
                dbCmd.Parameters.Add(new SqlParameter("@Status", data.Status));
                dbCmd.Parameters.Add(new SqlParameter("@MailTo", data.MailTo));
                dbCmd.Parameters.Add(new SqlParameter("@FullNameTo", data.FullNameTo));
                dbCmd.Parameters.Add(new SqlParameter("@AddressTo", data.AddressTo));
                dbCmd.Parameters.Add(new SqlParameter("@PhoneTo", data.PhoneTo));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("User");
            return true;
        }
        #endregion
        #region[User_Delete]
        public bool User_Delete(string Id)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_User_Delete", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@UId", Id));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("User");
            return true;
        }
        #endregion
        #region[User_GetByUsernamePass]
        public List<Data.UserInfo> User_GetByUserPass(string Username, string Pass)
        {
            return this.User_GetByAll().FindAll(u => u.Username == Username && u.Pass == Pass);
        }
        #endregion
        #region[User_isAdmin]
        public bool isAdmin(string Username,string Pass)
        {
            if (this.User_GetByUserPass(Username, Pass)[0].isAdmin == "1")
                return true;
            return false;
        }
        #endregion

        public bool User_UpdateStatus(string UId,string Status)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_User_UpdateStatus", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@UId", UId));
               
                dbCmd.Parameters.Add(new SqlParameter("@Status", Status));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("User");
            return true;
        }
    }
}